﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace app
{
    class LocalData 
    {
        private static string localDir;

        private static List<byte[]> addresses = new List<byte[]>();

        private static List<byte[]> waiting = new List<byte[]>();

        private static List<byte[]> non_confirmed = new List<byte[]>();

        private static Dictionary<byte[], IPEndPoint> in_transit = new Dictionary<byte[], IPEndPoint>();

        public static void Add(byte[] address, byte[] data)
        {
            addresses.Add(address);

            File.WriteAllBytes(Path.Combine(localDir, Convert.ToBase64String(address)), data);

            if (IsWaiting(address))
                RemoveWaiting(address);
        }

        public static byte[] Get(byte[] address)
        {
            foreach (byte[] b in addresses)
                if (Addresses.Compare(b, address) == 0)
                    return File.ReadAllBytes(Path.Combine(localDir, Convert.ToBase64String(address)));

            return null;
        }

        public static void Waiting(byte[] address)
        {
            waiting.Add(address);
        }

        public static bool IsWaiting(byte[] address)
        {
            foreach (byte[] b in waiting)
                if (Addresses.Compare(b, address) == 0)
                    return true;

            return false;
        }

        public static bool IsInTransit(byte[] address)
        {
            foreach (byte[] b in in_transit.Keys)
                if (Addresses.Compare(b, address) == 0)
                    return true;

            return false;
        }

        public static void RemoveWaiting(byte[] address)
        {
            for (int i = 0; i < waiting.Count(); i++)
            {
                if (Addresses.Compare(waiting[i], address) == 0)
                {
                    waiting.RemoveAt(i);
                    return;
                }
            }
        }

        public static IPEndPoint GetInTransit(byte[] address)
        {
            foreach (byte[] b in in_transit.Keys)
                if (Addresses.Compare(b, address) == 0)
                    return in_transit[b];

            return null;
        }

        public static void RemoveInTransit(byte[] address)
        {
            foreach (byte[] b in in_transit.Keys)
            {
                if (Addresses.Compare(b, address) == 0)
                {
                    waiting.Remove(b);

                    return;
                }
            }
        }

        public static void AddInTransit(byte[] address, IPEndPoint IPEndPoint)
        {
            in_transit.Add(address, IPEndPoint);
        }

        private static void Save()
        {
            //List<byte> bb = new List<byte>();

            //foreach(byte[] key in Data.Keys)
            //{
            //    bb.AddRange(key);
            //    bb.AddRange(BitConverter.GetBytes(Data[key].Length));
            //    bb.AddRange(Data[key]);
            //}

            //File.WriteAllBytes("data.txt", bb.ToArray());
        }

        public static void Load()
        {
            localDir = Path.Combine("Data", Convert.ToBase64String(Program.localPeer.Address));

            if (!Directory.Exists(localDir))
            {
                Directory.CreateDirectory(localDir);
                return;
            }

            string[] data = Directory.GetFiles(localDir, "*", SearchOption.AllDirectories);
            string t = string.Empty;

            foreach (string s in data)
            {
                t = Path.GetFileName(s);

                addresses.Add(Convert.FromBase64String(t));
            }
        }
    }
}
